package com.elric.mall.controller;

import com.elric.mall.common.api.CommonResult;
import com.elric.mall.model.UmsFileInfo;
import com.elric.mall.service.UmsFileInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 文件信息表 控制器
 *
 * @author yubaoshan
 * @date 2020/6/7 22:15
 */
@Api(tags = "UmsFileInfoController", description = "文件信息表 控制器")
@RequestMapping("/file")
@RestController
public class UmsFileInfoController {

    @Autowired
    private UmsFileInfoService umsFileInfoService;

    /**
     * 上传文件
     *
     * @author yubaoshan
     * @date 2020/6/7 22:15
     */
    @ApiOperation(value = "上传附件")
    @PostMapping("/upload")
    @ResponseBody
    public CommonResult upload(@RequestPart("file") MultipartFile file) {
        String fileId = umsFileInfoService.uploadFile(file);
        return CommonResult.success(fileId);
    }

    /**
     * 下载文件
     *
     * @author yubaoshan, xuyuxiang
     * @date 2020/6/9 21:53
     */
    @ApiOperation(value = "根据附件id下载附件")
    @GetMapping("/download")
    @ResponseBody
    public void download(@RequestParam(value = "fileId") String fileId, HttpServletResponse response) {
        umsFileInfoService.download(fileId, response);
    }

    /**
     * 文件预览
     *
     * @author yubaoshan, xuyuxiang
     * @date 2020/6/9 22:07
     */
    @ApiOperation(value = "根据附件id预览附件")
    @GetMapping("/preview")
    public void preview(@RequestParam(value = "fileId") String fileId, HttpServletResponse response) {
        umsFileInfoService.preview(fileId, response);
    }

    /**
     * 查询文件信息表
     *
     * @author yubaoshan
     * @date 2020/6/7 22:15
     */
    @ApiOperation(value = "查询文件信息表")
    @GetMapping("/list")
    @ResponseBody
    public CommonResult list(UmsFileInfo umsFileInfo) {
        return CommonResult.success(umsFileInfoService.list(umsFileInfo));
    }

    /**
     * 查看详情文件信息
     *
     * @author yubaoshan
     * @date 2020/6/7 22:15
     */
    @ApiOperation(value = "查看详情文件信息")
    @GetMapping("/detail")
    @ResponseBody
    public CommonResult detail(@RequestParam(value = "fileId") String fileId) {
        return CommonResult.success(umsFileInfoService.detail(fileId));
    }

    /**
     * 删除文件信息表
     *
     * @author yubaoshan
     * @date 2020/6/7 22:15
     */
    @PostMapping("/delete")
    @ResponseBody
    public CommonResult delete(@RequestParam(value = "fileId") String fileId) {
        umsFileInfoService.delete(fileId);
        return CommonResult.success("ok");
    }
}
